#!/bin/bash

install-docker() {
    current_dir=$(pwd)
    git clone https://gitee.com/caijize/docker-install.git
    cd docker-install && ./install-docker.sh && cd .. && rm -rf docker-install
    cd "$current_dir"
}

install-postgresql(){
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/postgres
    docker tag 8e4fc9e18489 postgres
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/postgres:latest
    mkdir -p /docker/pgsql/data
    mkdir -p /docker/pgsql/scripts
    docker run --name postgres --restart=always -e POSTGRES_PASSWORD=Pass1234 -p 5432:5432 \
        -v /docker/pgsql/data:/var/lib/postgresql/data \
        -v /docker/pgsql/scripts:/var/lib/postgresql/scripts \
        -d postgres
    sleep 10
    cp pgsql/*.sql /docker/pgsql/scripts/
    cp pgsql/loadfile.sh /docker/pgsql/scripts/
    docker exec -it postgres bash -c "cd /var/lib/postgresql/scripts/ && ./loadfile.sh"
}

install-redis(){
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/redis
    docker tag e0ce02f88e58 redis:latest
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/redis:latest
    docker run --restart=always -p 6379:6379 --name redis -d redis --requirepass Pass1234
    # docker exec -it redis redis-cli -a Pass1234 -n 1 set YSB:domain-PG "<?xml version='1.0' encoding='UTF-8' standalone='no'?><document type='freeswitch/xml'><section name='dialplan' description='dialplan'><context name='tranfer'><extension name='callin_020ivr'><condition field='destination_number' expression='^02088889999\$'><action application='export' data='nolocal:absolute_codec_string=PCMA'/><action application='lua' data='pactera_baojie/baojie_ivr_new.lua'/></condition></extension><extension name='transfer_02161242084'><condition field='destination_number' expression='^0?\\d{5,11}\$'><action application='export' data='nolocal:absolute_codec_string=PCMA'/><action application='export' data='origination_caller_id_number=02161242084'/><action application='bridge' data='sofia/internal/\${destination_number}@119.3.110.199:61001'/></condition></extension></context><context name='public'></context><context name='default'><extension name='default'><condition field='destination_number' expression='^02569783361\$'><action application='answer'/><action application='callcenter' data='support@default'/></condition></extension><extension name='callin_02161242084'><condition field='destination_number' expression='^013579\$'><action application='lua' data='pactera_yjt/yjt_ivr.lua'/></condition></extension><extension name='local_extension'><condition field='destination_number' expression='^([8-9][0-9][0-9][0-9][0-9])\$'><action application='export' data='dialed_extension=\$1'/><action application='set' data='ringback=\${us-ring}'/><action application='set' data='transfer_ringback=\$\${hold_music}'/><action application='set' data='call_timeout=30'/><action application='set' data='hangup_after_bridge=false'/><action application='set' data='continue_on_fail=true'/><action application='export' data='media_mix_inbound_outbound_codecs=true'/><action application='export' data='RECORD_STEREO=true'/><action application='export' data='execute_on_answer=record_session /recording/\${strftime(%Y)}/\${strftime(%m)}/\${strftime(%d)}/\${caller_id_number}.\${strftime(%Y%m%d%H%M%S)}.mp3'/><action application='export' data='RECORD_BRIDGE_REQ=true'/><action application='bridge' data='user/\${dialed_extension}@domain-PG'/><action application='answer'/></condition></extension></context></section></document>"
}

install-nginx(){
    current_dir=$(pwd)
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/nginx
    docker tag 89da1fb6dcb9 nginx:latest
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/nginx:latest

    # mkdir -p /docker/nginx

    # cd /docker/nginx

    # docker run -p 80:80 -d --name nginx nginx

    # docker cp nginx:/etc/nginx/nginx.conf .
    # docker cp nginx:/etc/nginx/conf.d .
    # docker cp nginx:/usr/share/nginx/html .

    # docker rm -f  nginx

    # sed -i 's/user[[:space:]]*nginx;/user  root;/g' nginx.conf

    mkdir -p /docker/
    cd /docker
    git clone https://gitee.com/caijize/nginx-data.git
    cd /docker/nginx-data

    docker run --name nginx --network host --restart always \
    -v /docker/nginx-data/nginx.conf:/etc/nginx/nginx.conf \
    -v  /docker/nginx-data/conf.d:/etc/nginx/conf.d \
    -v  /docker/nginx-data/html:/usr/share/nginx/html \
    -v  /recordings:/recordings \
    --env=TZ=Asia/Shanghai \
    --env=LANG=zh_CN.UTF-8 \
    --privileged=true \
    -t -d nginx

    cd "$current_dir"

    # cp nginx/nginx.reload.sh /docker/nginx
    # cp nginx/recordings.conf /docker/nginx/conf.d
    # chmod +x /docker/nginx/nginx.reload.sh
    # /docker/nginx/nginx.reload.sh
}

install-rabbitmq(){
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/rabbitmq
    docker tag 9aa53bdfa924 rabbitmq:latest
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/rabbitmq:latest
    
    docker run -d -p 15672:15672 -p 5672:5672 \
    --restart=always \
    -e RABBITMQ_DEFAULT_VHOST=/  \
    -e RABBITMQ_DEFAULT_USER=admin \
    -e RABBITMQ_DEFAULT_PASS=admin123456 \
    --env=TZ=Asia/Shanghai \
    --env=LANG=zh_CN.UTF-8 \
    --hostname myRabbit \
    --name rabbitmq\
    rabbitmq:latest

    docker exec -it rabbitmq rabbitmq-plugins enable rabbitmq_management

    sleep 10

    # docker exec -it rabbitmq rabbitmqctl add_vhost /
    # docker exec -it rabbitmq rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
}

install-etcd(){
    current_dir=$(pwd)
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/etcd:v3.3
    docker tag 1e3509b14de0 etcd:v3.3
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/etcd:v3.3
    cd /docker/
    git clone https://gitee.com/caijize/etcd.git
    cd etcd/ && mkdir etcd-data && docker-compose up -d && cd ..
    cd "$current_dir"
    #git checkout all
    ##wget https://github.com/evildecay/etcdkeeper/releases/download/v0.7.6/etcdkeeper-v0.7.6-linux_x86_64.zip
    #unzip etcdkeeper-v0.7.6-linux_x86_64.zip
    #chmod +x etcdkeeper/etcdkeeper
    #etcdkeeper/etcdkeeper -p 8023
    
    yum install unzip -y
    wget http://caijize.top/etcdkeep.zip
    unzip etcdkeep.zip
    chmod +x etcdkeeper/etcdkeeper
    rm etcdkeep.zip

    docker exec -it etcd etcdctl put /Config/office/Redis "{\"Addr\":\"127.0.0.1:6379\",\"Pass\":\"Pass1234\",\"Db\":1,\"MaxRetries\":0,\"PoolSize\":0,\"MinIDleConns\":0,\"TimeOut\":0,\"Sentinel\":{\"MasterName\":\"\",\"SentinelAddrs\":null,\"SentinelPassword\":\"\"}}"
    docker exec -it etcd etcdctl put /Config/office/PgSQL "{\"Read\":[{\"Host\":\"127.0.0.1\",\"Port\":5432,\"User\":\"postgres\",\"Pass\":\"Pass1234\",\"Name\":\"fsw\"}],\"Write\":[{\"Host\":\"127.0.0.1\",\"Port\":5432,\"User\":\"postgres\",\"Pass\":\"Pass1234\",\"Name\":\"fsw\"}],\"Base\":{\"MaxOpenConn\":50,\"MaxIDleConn\":5,\"ConnMaxLifeTime\":5}}"
    docker exec -it etcd etcdctl put /Config/office/RabbitMQ '{"url":"amqp://admin:admin123456@127.0.0.1:5672/%2F"}'
}

install-homer(){
    current_dir=$(pwd)
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/node-exporter
    docker tag 0c6f6c1bdd47 prom/node-exporter
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/node-exporter:latest
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/prometheus
    docker tag ecb74a3b23a9 prom/prometheus
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/prometheus:latest
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/alertmanager
    docker tag 11f11916f8cd prom/alertmanager
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/alertmanager:latest
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/loki
    docker tag 105db0731131 grafana/loki
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/loki:latest
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/grafana:master
    docker tag 1ab9b06fdf6a grafana/grafana:master
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/grafana:master
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/heplify-server
    docker tag a12c78398507 sipcapture/heplify-server
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/heplify-server:latest
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/webapp
    docker tag e89127b1976d sipcapture/webapp
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/webapp:latest
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/postgres:11-alpine
    docker tag 10d7fb41183a postgres:11-alpine
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/postgres:11-alpine
    mkdir -p /docker
    cd /docker
    git clone https://gitee.com/caijize/homer7-docker.git
    cd homer7-docker/heplify-server/hom7-prom-all
    docker-compose up -d    
    cd "$current_dir"
}

rm-homer(){
    docker rm -f db
    docker rm -f homer-webapp
    docker rm -f heplify-server
    docker rm -f nodeexporter
    docker rm -f alertmanager
    docker rm -f prometheus
    docker rm -f loki
    docker rm -f grafana
}

install-mrcp-server(){
    current_dir=$(pwd)

    mkdir -p /docker/nls-cloud-sdm
    cd /docker/nls-cloud-sdm

    if docker ps -a --format '{{.Names}}' | grep -q '^nls-cloud-sdm$'; then
        docker rm -f nls-cloud-sdm
    fi

    docker run -d --privileged --net=host --name nls-cloud-sdm \
    -v `pwd`/logs:/home/admin/logs \
    -v `pwd`/data:/home/admin/disk \
    registry.cn-shanghai.aliyuncs.com/nls-cloud/sdm:latest standalone

    cd "$current_dir"
}

install-debian(){
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/debian:12
    docker tag 5027089adc4ce4ec debian:12
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/debian:12
}

install-alpin(){
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/alpine
    docker tag b0c9d60fc5e3fa23 alpine
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/alpine
}

install-asrlogget(){
    current_dir=$(pwd)

    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/asr_get_app
    docker tag 02b3a29e8034 callcenter/asr_get_app
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/asr_get_app

    mkdir -p /docker/
    cd /docker
    git clone https://gitee.com/caijize/asrlogget.git
    cd asrlogget

    docker build -t asr_get_a
    mkdir -p /docker/asrlogget/log/

    docker run -dit --network host --restart always --name=asrlogget \
    -v /docker/asrlogget/log/:/opt/app/log/ \
    -v /docker/nls-cloud-sdm/logs/nls-cloud-sdm/:/opt/sdm-logs/  \
    -w /opt/app/ callcenter/asr_get_app 

    cd "$current_dir"
}

install-ddffs(){
    current_dir=$(pwd)

    install-alpin

    mkdir -p /docker
    cd /docker
    git clone https://gitee.com/caijize/ddffs-data.git
    cd ddffs-data
    mkdir log

    docker run -dit --network host --restart always --name=ddffs \
    -v /docker/ddffs-data/:/opt/ddffs/  \
    --log-driver=loki  \
    --log-opt loki-url="http://127.0.0.1:3100/loki/api/v1/push"  \
    --log-opt loki-retries=5  \
    --log-opt loki-batch-size=400  \
    --log-opt max-size=50m  \
    --log-opt max-file=3  \
    --privileged=true  \
    -w /opt/ddffs/ alpine ./ddffs


    cd "$current_dir"
}

# install-fswatch(){
#     current_dir=$(pwd)

#     install-debian

#     mkdir -p /docker
#     cd /docker
#     git clone https://gitee.com/caijize/fswatch.git
#     cd fswatch
#     mkdir log

#     docker run -dit --network host --restart always --name=fswatch \
#     -v /docker/fswatch/:/opt/fswatch/  \
#     --log-driver=loki  \
#     --log-opt loki-url="http://127.0.0.1:3100/loki/api/v1/push"  \
#     --log-opt loki-retries=5  \
#     --log-opt loki-batch-size=400  \
#     --log-opt max-size=50m  \
#     --log-opt max-file=3  \
#     --env=TZ=Asia/Shanghai \
#     --env=LANG=zh_CN.UTF-8 \
#     --privileged=true  \
#     -w /opt/fswatch/ alpine ./fswatch

#     cd "$current_dir"
# }

install-fswatch(){
    current_dir=$(pwd)

    install-alpin

    mkdir -p /docker
    cd /docker
    git clone https://gitee.com/caijize/fswatch.git
    cd fswatch
    mkdir log

    docker run -dit --network host --restart always --name=fswatch \
    -v /docker/fswatch/:/opt/fswatch/  \
    --log-driver=loki  \
    --log-opt loki-url="http://127.0.0.1:3100/loki/api/v1/push"  \
    --log-opt loki-retries=5  \
    --log-opt loki-batch-size=400  \
    --log-opt max-size=50m  \
    --log-opt max-file=3  \
    --env=TZ=Asia/Shanghai \
    --env=LANG=zh_CN.UTF-8 \
    --privileged=true  \
    -w /opt/fswatch/ alpine ./fswatch

    cd "$current_dir"
}

install-ctiserver(){
    current_dir=$(pwd)

    install-alpin

    mkdir -p /docker
    cd /docker
    git clone https://gitee.com/caijize/cti-server.git
    cd cti-server
    mkdir log

    docker run -dit --network host --restart always --name=ctiserver \
    -v /docker/cti-server/:/opt/ctiserver/  \
    --log-driver=loki  \
    --log-opt loki-url="http://127.0.0.1:3100/loki/api/v1/push"  \
    --log-opt loki-retries=5  \
    --log-opt loki-batch-size=400  \
    --log-opt max-size=50m  \
    --log-opt max-file=3  \
    --env=TZ=Asia/Shanghai \
    --env=LANG=zh_CN.UTF-8 \
    --privileged=true  \
    -w /opt/ctiserver/ alpine ./fswatch

    cd "$current_dir"
}

install-freeswitch(){
    current_dir=$(pwd)
    docker pull registry.cn-shenzhen.aliyuncs.com/call-center/fs-1.10.5
    docker tag 1b6b346674ef fs-cc
    docker rmi registry.cn-shenzhen.aliyuncs.com/call-center/fs-1.10.5

    mkdir -p /docker
    cd /docker
    git clone https://gitee.com/caijize/freeswitch_data.git
    cd freeswitch_data
    mkdir record
    mkdir log

    docker run --name fs-cc --network host --restart always \
    -v /docker/freeswitch_data/scripts/:/usr/local/freeswitch/scripts/ \
    -v /docker/freeswitch_data/conf/:/usr/local/freeswitch/conf/ \
    -v /docker/freeswitch_data/log/:/usr/local/freeswitch/log/ \
    -v /docker/freeswitch_data/record:/recordings  \
    -v /docker/freeswitch_data/odbc.ini:/etc/odbc.ini  \
    -v /docker/freeswitch_data/odbcinst.ini:/etc/odbcinst.ini  \
    --env=TZ=Asia/Shanghai \
    --env=LANG=zh_CN.UTF-8 \
    --log-driver=loki  \
    --log-opt loki-url="http://127.0.0.1:3100/loki/api/v1/push"  \
    --log-opt loki-retries=5  \
    --log-opt loki-batch-size=400  \
    --log-opt max-size=50m  \
    --log-opt max-file=3  \
    --privileged=true \
    -t -d fs-cc freeswitch -nonat

    cd "$current_dir"
}

test-func(){
    echo "测试函数调用"
}

if [ $(id -u) -eq 0 ]; then
    echo "权限校验通过."
else
    echo "权限校验不通过，当前用户没有root权限."
    exit 1
fi

# 检查参数个数是否大于0
if [ $# -gt 0 ]; then    
    # 检查特定参数是否存在
    while [ "$1" != "" ]; do
        case $1 in
            # -p | --param )  # 检查是否存在 -p 或 --param 参数
            #     echo "存在 -p 或 --param 参数"
            #     shift ;;
            --al )  #安装asrlogget
                echo "安装asrlogget"
                install-asrlogget
                shift ;;
            -c | --isf )  # 安装ctiserver
                echo "安装ctiserver"
                install-ctiserver
                shift ;;
            -d | --isd )  # 安装ddffs
                echo "安装ddffs"
                install-ddffs
                shift ;;
            -f | --isf )  # 安装FS
                echo "安装FS"
                install-freeswitch
                shift ;;
            -H | --ish )  # 安装homer
                echo "安装homer"
                install-homer
                shift ;;
            -e | --ise )  # 安装部署etcd
                echo "安装部署etcd"
                install-etcd
                shift ;;
            -i | --idk )  # 安装Docker
                echo "安装Docker"
                install-docker
                shift ;;
            -M | --ism )  # 安装部署mrcp-server
                echo "安装部署mrcp-server"
                install-mrcp-server
                shift ;;
            -n | --isn )  # 安装部署nginx
                echo "安装部署nginx"
                install-nginx
                shift ;;
            -p | --isp )  # 安装部署postgresql
                echo "安装部署postgresql"
                install-postgresql
                shift ;;
            -R | --isR )  # 安装部署Rabbitmq
                echo "安装部署Rabbitmq"
                install-rabbitmq
                shift ;;
            -r | --isr )  # 安装部署redis
                echo "安装部署redis"
                install-redis
                shift ;;
            --rmh )  #删除homer
                echo "删除homer"
                rm-homer
                shift ;;
            -w | --isw )  # 安装fswatch
                echo "安装fswatch"
                install-fswatch
                shift ;;
            * )  # 其他情况
                echo "其他情况"
                echo $1
                shift ;;
        esac
    done
else
    echo "没有参数传递给脚本"
fi

cd ..
rm -rf callcenter
